package org.googlecode.lightning.search.search;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

/**
 * highlighter
 * 
 * @author daragon
 * 
 */
public class Highlight extends Highlighter {

	private Highlight(Formatter formatter, Scorer fragmentScorer) {
		super(formatter, fragmentScorer);
	}

	public final static String START = "<span class=\"highlighter\">";
	public final static String END = "</span>";

	public static Highlight getInstance(Query query, Fragmenter textFragmenter) {

		Highlight highlight = getCustomInstance(query, START, END);
		highlight.setTextFragmenter(textFragmenter);
		return highlight;
	}

	/**
	 * custom
	 * 
	 * @param query
	 * @param prefix
	 * @param suffix
	 * @return
	 */
	private static Highlight getCustomInstance(Query query, String prefix,
			String suffix) {
		SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(
				prefix, suffix);

		Highlight highlight = new Highlight(simpleHTMLFormatter,
				new QueryScorer(query));
		return highlight;
	}
}
